package com.example.demo.application.service;

import com.example.demo.application.dto.UserDTO;
import com.example.demo.application.dto.UserRequest;
import com.example.demo.application.mapper.UserMapper;
import com.example.demo.domain.model.User;
import com.example.demo.domain.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserApplicationService {

    private final UserService userService;
    private final UserMapper userMapper;

    public UserDTO createUser(UserRequest request) {
        User user = userService.createUser(request.getUsername(), request.getEmail(), request.getPassword());
        return userMapper.toDTO(user);
    }

    public Optional<UserDTO> getUserById(Long id) {
        return userService.getUserById(id)
                .map(userMapper::toDTO);
    }

    public List<UserDTO> getAllUsers() {
        return userService.getAllUsers().stream()
                .map(userMapper::toDTO)
                .collect(Collectors.toList());
    }

    public UserDTO updateUser(Long id, UserRequest request) {
        // 对于更新操作，我们不更新密码，只更新用户名和邮箱
        User user = userService.updateUser(id, request.getUsername(), request.getEmail());
        return userMapper.toDTO(user);
    }

    public void deleteUser(Long id) {
        userService.deleteUser(id);
    }
}